package com.forever7776.life.admin.controller.life.m.send.email;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.enums.EmailTemplateTypeEnum;
import com.forever7776.life.core.entity.send.SendEmailTemplate;
import com.forever7776.life.core.service.send.ISendEmailTemplateService;
import enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

/**
 * <p>
 * 系统邮件模板
 * </p>
 *
 * @author kz
 * @date 2018-03-08
 */
@Controller
@RequestMapping("/email/template")
public class SendEmailTemplateController extends BaseController {

    @Autowired
    private ISendEmailTemplateService iSendEmailTemplateService;

    @RequestMapping
    public String index() {
        return LIFE_ADMIN_VIEW_PAGE_PATH + "send/email/email_template";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject list(@RequestParam("title") String title, @RequestParam("status") Integer status) {
        JSONObject result = new JSONObject();
        Page<SendEmailTemplate> p = new Page<SendEmailTemplate>();
        p.setCurrent(pageNumber);
        Page<SendEmailTemplate> page = iSendEmailTemplateService.selectAll(p, title, status);
        result.put("page", page);
        return result;
    }

    /**
     * 查看邮件模板
     *
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public String view(@PathVariable Integer id, ModelMap map) {
        SendEmailTemplate template = iSendEmailTemplateService.selectById(id);
        map.put("template", template);
        map.put("EMAIL_TEMPLATE_TYPE_MAP", EmailTemplateTypeEnum.TYPE_MAP);
        return LIFE_ADMIN_VIEW_PAGE_PATH + "send/email/email_template_view";
    }


    /**
     * 保存修改博客
     *
     * @param template
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject edit(@PathVariable Integer id, SendEmailTemplate template) {
        if (iSendEmailTemplateService.selectById(id) == null) {
            return ResultUtil.getFailResult("该邮件模板不存在!");
        }
        JSONObject result = checkForm(template, "");
        if (result.get("code").equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        iSendEmailTemplateService.updateById(template);
        return result;
    }


    /**
     * 创建邮件模板页面
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        map.put("EMAIL_TEMPLATE_TYPE_MAP", EmailTemplateTypeEnum.TYPE_MAP);
        return LIFE_ADMIN_VIEW_PAGE_PATH + "send/email/email_template_add";
    }


    /**
     * 添加邮件模板
     *
     * @param t
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addRole(SendEmailTemplate t) {
        JSONObject result = checkForm(t, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        iSendEmailTemplateService.insert(t);
        return result;
    }

    /**
     * 冻结邮件模板
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/frozen/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject frozenRole(@PathVariable String id, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        iSendEmailTemplateService.frozen(id, status);
        return ResultUtil.getSuccessResult("操作成功！");
    }

    public JSONObject checkForm(SendEmailTemplate t, String msg) {
        if (StringUtils.isEmpty(t.getTitle())) {
            return ResultUtil.getFailResult("邮件模板标题为空");
        }
        if (StringUtils.isEmpty(t.getContent())) {
            return ResultUtil.getFailResult("邮件模板内容为空");
        }
        if(StringUtils.isEmpty(t.getType())){
            return ResultUtil.getFailResult("邮件模板类型为空");
        }
        return ResultUtil.getSuccessResult(msg);
    }

}
